<!DOCTYPE html> <html lang=zh-CN style><!--
 Page saved with SingleFile 
 url: http://c.biancheng.net/view/9374.html 
 saved date: Mon May 29 2023 20:35:37 GMT+0800 (中国标准时间)
--><meta charset=utf-8>
<meta http-equiv=X-UA-Compatible content="IE=edge">
<meta name=viewport content="width=device-width, initial-scale=1">
<meta name=renderer content=webkit>
<meta name=force-rendering content=webkit>
<meta name=applicable-device content=pc,mobile>
<meta name=MobileOptimized content=width>
<meta name=HandheldFriendly content=true>
<meta http-equiv=Cache-Control content=no-transform>
<meta http-equiv=Cache-Control content=no-siteapp>
<meta name=format-detection content="telephone=no">
<style>html{min-height:100%;width:100%}body{margin:0px;padding:0px;min-height:100%;width:100%;font:14px"Helvetica Neue","微软雅黑","Microsoft Yahei",Helvetica,Arial,sans-serif;color:#444;background-color:#fff}h1,h2,h4,p,ol,ul,pre{margin-top:0px;margin-bottom:0px}ul,ol,dd{list-style:none;margin-left:0px;padding-left:0px}a,img{border:none}a{text-decoration:none}a:hover{text-decoration:underline}.hover-none:hover,.hover-none a:hover{text-decoration:none}code{display:inline-block;margin:auto 2px;padding:0px 4px;font-size:100%;color:#333;border:1px solid #ddd;background:#f6f6f6;border-radius:2px;font-family:"Helvetica Neue","微软雅黑","Microsoft Yahei",Helvetica,Arial,sans-serif}.tip-box{margin:10px 0px;padding:10px;border-left:4px solid #ddd;color:#666;background-color:#f9f9f9;border-radius:4px;font-size:14px;word-wrap:break-word}.left{display:block;float:left}.right{display:block;float:right}.clearfix:after{content:"​";display:block;height:0;clear:both}.col-green{color:#008000}.col-link{color:#007dbb}@font-face{font-family:"iconfont";src:url(data:font/woff2;base64,d09GMgABAAAAAA4YAAsAAAAAGNAAAA3KAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHFQGYACFcgqfaJl/ATYCJANUCywABCAFhGcHgh8b+BQzo3aLtJIn+z8ccHIvljtCIinq9JXpmjoy0UiuppKmK1hcjwxycA/D0rAhW7L8WG/cxwKYj/6uyHW9co7Lfj+UEv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4iRlr5HQtDcUwrNsbyvQAwnrfypRDurmE9LU0t0nQ3Jrov1ZaqfK8+MxmBnDG3azPrxszsnLyCsqUq1CpSrUaterUa9CoSbOiFkjO0FkdL5xPQOv3MIOjQXoTPwEbpKzTA23aCDT1OxbJFpEL2O8ckC2jq4XjLj/N0SQm62LjyW4ufEpwN2vfd68HEqceOOtA/+dpL2VVhzJAb94IneOY8Kwk0m8M1CZaGbb2PpzHkkmTrVI8YCf5OZLmNj0YmTu0ElhJcUSac4GLyK2nbRe5WZcMAQA=)format("woff2")}.iconfont{font-family:"iconfont"!important;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.iconfont-book:before{content:""}.iconfont-vip:before{content:""}.iconfont-home:before{content:""}.iconfont-home2:before{content:""}.iconfont-user:before{content:""}.iconfont-list-vertical:before{content:""}.iconfont-close:before{content:""}.iconfont-c-course:before{content:""}.iconfont-java-course:before{content:""}#topbar{height:36px;line-height:36px;background-color:#335070;border-bottom:1px solid #2f4b69;overflow:visible}#product-type li{float:left}#product-type li.active{height:37px;background-color:#375a7f}#product-type li a{display:inline-block;padding:0px 15px;color:#f9f9f9}#product-type li a .iconfont{padding-right:3px}#ggxc-arctop-pc-1{margin-top:10px}#ggxc-arctop-pc-1 p.small-image{display:flex;flex-wrap:wrap;justify-content:space-between}#ggxc-arctop-pc-1 p.small-image>a{width:49%}#ggxc-arctop-pc-1 p.big-link{padding:8px 10px;margin-top:8px;border:1px #d0d0d0 dashed;background-color:#f9f9f9;line-height:1.6em;border-radius:4px}#ggxc-weixin-arcbottom{margin:20px auto 10px auto;line-height:1.6em}#header{height:42px;width:100%;top:0px;background-color:#375a7f;color:#f3f3f3;z-index:1000;overflow:hidden}#header a{color:#f3f3f3}#logo{height:26px;width:108px;padding:8px 20px}#nav-main li{float:left;line-height:42px}#nav-main li a{display:block;margin-right:10px;padding:0px 6px}.user-info{display:block;float:right;height:42px;margin-right:20px;padding:0px 6px;line-height:42px;font-size:16px}#main{min-width:100%}#sidebar{float:left;padding-bottom:40px;width:20%;border-right:1px solid #ddd}#article-wrap{float:left;padding-bottom:40px;width:79%;margin-left:-1px;border-left:1px solid #ddd}#article{margin-left:20px;width:80%}.arc-info{position:relative;margin-top:18px;padding-bottom:6px;line-height:13px;color:#666;font-size:13px}.arc-info a{color:#666}.arc-info .position{display:block;margin-right:110px}.pre-next-page{padding:8px 0px;line-height:16px;border-bottom:1px solid #ddd;border-top:1px solid #ddd}.pre-next-page a{color:#007dbb}.pre-next-page a .icon{font-size:16px}h1{font-size:26px;line-height:1.6em;font-weight:normal}#arc-body{margin:10px auto;line-height:1.6em}#arc-body h2{margin:15px auto;font-size:24px;line-height:1.6}#arc-body h4{margin:8px auto;font-size:14px;line-height:1.6}#arc-body ul{list-style:disc;padding-left:20px}#arc-body ol{list-style:decimal;padding-left:20px}.box-bottom{border:1px solid #ddd;margin-top:20px}.box-bottom h4{padding-left:12px;background-color:#eee;border-bottom:1px solid #ddd;line-height:2.8em}#nice-arcs ul{padding:7px 0px}#nice-arcs li{width:49%;float:left;height:28px;line-height:28px;overflow:hidden}#nice-arcs li a{display:inline-block;margin:0px 12px}#nice-arcs a{color:#444}#contents dt{height:40px;margin-bottom:6px;padding-left:6px;line-height:40px;font-weight:normal;border-bottom:1px solid #e1e1e1;border-top:1px solid #e1e1e1;background-color:#f8f8f8}#contents dt:first-child{margin-top:0px}#contents dt a{color:#333}#contents dd{margin:auto 12px;line-height:26px}#contents dd a{color:#333}#contents dd.active,#contents dd.active a{color:#D33428}#contents dd span.channel-num{display:block;float:left;margin-top:5px;margin-right:6px;height:16px;width:16px;line-height:16px;text-align:center;font-size:12px;background-color:#777;color:#fff}#footer{padding:20px 90px 20px 30px;border-top:1px solid #e5e5e5;background-color:#f5f5f5}#footer p{font-size:13px;color:#666;line-height:1.6em}#footer p a{color:#3c8cb5}#footer p span{display:inline-block;padding:0px 5px;font-size:12px;color:#777}#logo_bottom{margin-top:8px}#return-top{display:block;position:fixed;right:30px;bottom:30px;height:50px;width:50px;line-height:50px;text-align:center;color:#375a7f;font-size:18px;background-color:#eee;border:1px solid #ddd;border-radius:3px;cursor:pointer;font-family:SimHei}#addweixin-widget{position:fixed;right:10px;top:30%;text-align:center;background-color:#fff}#addweixin-widget p{width:120px;padding:16px 10px 10px 10px;border:1px solid #ccc;border-radius:4px;color:#55ab55}#close-addweixin-widget{display:inline-block;margin-top:4px;font-size:24px;color:#666;cursor:pointer}pre{display:block;padding:0px;margin:0px;font-size:14px;line-height:1.6em;color:#666;white-space:pre-wrap;word-wrap:break-word;background:none;border:none;border-radius:0px}.snippet-wrap{position:relative;margin:5px auto;padding:5px 0px;background-color:#fcfcfc;border:1px solid #ddd;border-radius:3px}.snippet-wrap .snippet-menu{position:absolute;top:0px;right:0px;background-color:#eee;border-left:1px solid #ddd;border-bottom:1px solid #ddd;border-radius:0px 3px 0px 3px;z-index:1}.snippet-wrap .snippet-num{padding:3px 8px 3px 45px!important;background:url()repeat-y}.snippet-wrap .snippet-num li{padding-left:8px;word-wrap:break-word;list-style:decimal-leading-zero outside none}.snippet-wrap .snippet-textonly{padding:3px 10px}.sh_bright .sh_sourceCode .sh_keyword{color:#ff3030;font-weight:bold}.sh_bright .sh_sourceCode .sh_type{color:#CF9511}.sh_bright .sh_sourceCode .sh_string{color:#1861a7}.sh_bright .sh_sourceCode .sh_comment{color:#38ad24}.sh_bright .sh_sourceCode .sh_number{color:#32ba06}.sh_bright .sh_sourceCode .sh_preproc{color:#5374b0}.sh_bright .sh_sourceCode .sh_symbol{color:#3030ee}.sh_bright .sh_sourceCode .sh_function{color:#d11ced}.sh_bright .sh_sourceCode .sh_cbracket{color:#3030ee}@media all and (max-width:1250px){#sidebar{width:27%}#article-wrap{width:72%}#article{width:90%}#footer{padding-right:70px}#return-top{right:20px;bottom:20px;height:36px;width:36px;line-height:36px;font-size:16px}#addweixin-widget{display:none}}@media all and (max-width:1200px){#footer{padding:20px}#footer .info{float:none;text-align:center}#footer .info p{margin-top:6px}#logo_bottom{float:none;margin:10px auto 0px auto}}@media all and (max-width:1100px){#sidebar{width:25%}#article-wrap{width:74%}#return-top{right:5px}}@media all and (max-width:769px){body{font-size:16px}#article-wrap{float:none;width:100%;margin-left:0px;border-left:none}#article{margin-left:0px;width:100%}#arc-body{padding:0px 10px;overflow-x:scroll}#sidebar{padding-bottom:8px}#topbar,#ggxc-arctop-pc-1{display:none}#header{height:auto;overflow:visible}#logo{float:none;padding:11px 20px;margin:auto;text-align:center}#nav-main{float:none;padding:4px 0px;border-top:1px solid #2f4b69;background-color:#456c96}#nav-main li{line-height:32px}#nav-main li a{padding:0px 10px;margin-right:0px}.toggle-target{display:none;width:100%!important}.user-info{display:block;position:absolute;top:0px;right:0px;float:none;height:48px;margin-right:0px;padding:0px 15px;line-height:48px;font-size:16px}.arc-info{margin-top:0px;padding:12px 10px;border-bottom:1px solid #ddd;background-color:#f6f6f6}.arc-info .position{white-space:nowrap;overflow:hidden;overflow-x:scroll;-webkit-overflow-scrolling:touch}.pre-next-page{padding:10px 10px;font-size:16px;line-height:18px}.pre-next-page .icon{font-size:18px}h1{padding:0px 10px}#ggxc-weixin-arcbottom{padding:0px 10px;font-size:16px}.box-bottom h4{line-height:2.5em}#nice-arcs li{width:100%;float:none}#sidebar{float:none;background-color:#f6f6f6;border-bottom:1px solid #ddd;border-right:none}pre{overflow:scroll}}</style>
<title>JS严格模式（use strict）详解</title>
<meta name=description content="JS严格模式使用use strict来指明。JS严格模式下，代码语法将更加严谨，将不再允许使用那些不严谨的语法。一些在正常模式下能够运行的代码，在严格模式下将不能运行。">
<link rel="shortcut icon" href=""><style>.sf-hidden{display:none!important}</style><link rel=canonical href=http://c.biancheng.net/view/9374.html><meta http-equiv=content-security-policy content="default-src 'none'; font-src 'self' data:; img-src 'self' data:; style-src 'unsafe-inline'; media-src 'self' data:; script-src 'unsafe-inline' data:; object-src 'self' data:; frame-src 'self' data:;"><style>img[src="data:,"],source[src="data:,"]{display:none!important}</style></head>
<body>
<div id=topbar class=clearfix>
<ul id=product-type class=left>
<li>
<a href=http://c.biancheng.net/><span class="iconfont iconfont-home"></span>首页</a>
</li>
<li class=active>
<a href=http://c.biancheng.net/sitemap/ rel=nofollow><span class="iconfont iconfont-book"></span>教程</a>
</li>
<li>
<a href=http://vip.biancheng.net/p/vip/show.php rel=nofollow target=_blank><span class="iconfont iconfont-vip"></span>VIP会员</a>
</li>
<li>
<a href="https://www.54benniao.com/c_course/?from=biancheng" target=_blank><span class="iconfont iconfont-c-course"></span>C语言高级课程</a>
</li>
<li>
<a href="https://www.54benniao.com/java_course/?from=biancheng" target=_blank><span class="iconfont iconfont-java-course"></span>Java高级课程</a>
</li>
</ul>
</div>
<div id=header class=clearfix>
<a id=logo class=left href=http://c.biancheng.net/>
<img height=26 src="" alt=C语言中文网>
</a>
<ul id=nav-main class="hover-none left clearfix">
<li class="wap-yes sf-hidden"></li>
<li><a href=http://c.biancheng.net/c/>C语言教程</a></li>
<li><a href=http://c.biancheng.net/cplus/>C++教程</a></li>
<li><a href=http://c.biancheng.net/python/>Python教程</a></li>
<li><a href=http://c.biancheng.net/java/>Java教程</a></li>
<li><a href=http://c.biancheng.net/linux_tutorial/>Linux入门</a></li>
<li><a href=http://c.biancheng.net/sitemap/ title=网站地图>更多&gt;&gt;</a></li>
</ul>
<span id=sidebar-toggle class="toggle-btn sf-hidden" toggle-target=#sidebar>目录 </span>
<a href="http://vip.biancheng.net/?from=topbar" class="user-info iconfont iconfont-user hover-none" target=_blank rel=nofollow title=用户中心></a>
</div>
<div id=main class=clearfix>
<div id=sidebar class=toggle-target>
<div id=contents>
<dt><span class="iconfont iconfont-list-vertical" aria-hidden=true></span><a href=http://c.biancheng.net/js/>JavaScript</a></dt>
<dd>
<span class=channel-num>1</span>
<a href=http://c.biancheng.net/view/5351.html>JavaScript是什么</a>
</dd>
<dd>
<span class=channel-num>2</span>
<a href=http://c.biancheng.net/view/9338.html>Node.js是什么</a>
</dd>
<dd>
<span class=channel-num>3</span>
<a href=http://c.biancheng.net/view/5355.html>第一个JavaScript程序</a>
</dd>
<dd>
<span class=channel-num>4</span>
<a href=http://c.biancheng.net/view/5359.html>JavaScript中的几个重要概念</a>
</dd>
<dd>
<span class=channel-num>5</span>
<a href=http://c.biancheng.net/view/5370.html>JavaScript注释</a>
</dd>
<dd>
<span class=channel-num>6</span>
<a href=http://c.biancheng.net/view/9339.html>JavaScript变量</a>
</dd>
<dd>
<span class=channel-num>7</span>
<a href=http://c.biancheng.net/view/9340.html>JavaScript数据类型</a>
</dd>
<dd>
<span class=channel-num>8</span>
<a href=http://c.biancheng.net/view/9341.html>JavaScript运算符</a>
</dd>
<dd>
<span class=channel-num>9</span>
<a href=http://c.biancheng.net/view/9342.html>JavaScript输出</a>
</dd>
<dd>
<span class=channel-num>10</span>
<a href=http://c.biancheng.net/view/5505.html>JavaScript if else</a>
</dd>
<dd>
<span class=channel-num>11</span>
<a href=http://c.biancheng.net/view/5526.html>JavaScript switch case</a>
</dd>
<dd>
<span class=channel-num>12</span>
<a href=http://c.biancheng.net/view/9343.html>JavaScript while</a>
</dd>
<dd>
<span class=channel-num>13</span>
<a href=http://c.biancheng.net/view/9344.html>JavaScript do while</a>
</dd>
<dd>
<span class=channel-num>14</span>
<a href=http://c.biancheng.net/view/9345.html>JavaScript for</a>
</dd>
<dd>
<span class=channel-num>15</span>
<a href=http://c.biancheng.net/view/9346.html>JavaScript for in</a>
</dd>
<dd>
<span class=channel-num>16</span>
<a href=http://c.biancheng.net/view/9347.html>JavaScript for of</a>
</dd>
<dd>
<span class=channel-num>17</span>
<a href=http://c.biancheng.net/view/9348.html>JavaScript跳出循环</a>
</dd>
<dd>
<span class=channel-num>18</span>
<a href=http://c.biancheng.net/view/9349.html>JavaScript函数</a>
</dd>
<dd>
<span class=channel-num>19</span>
<a href=http://c.biancheng.net/view/9351.html>JavaScript事件处理</a>
</dd>
<dd>
<span class=channel-num>20</span>
<a href=http://c.biancheng.net/view/9352.html>JavaScript作用域</a>
</dd>
<dd>
<span class=channel-num>21</span>
<a href=http://c.biancheng.net/view/9353.html>JavaScript对象</a>
</dd>
<dd>
<span class=channel-num>22</span>
<a href=http://c.biancheng.net/view/9354.html>JavaScript Number对象</a>
</dd>
<dd>
<span class=channel-num>23</span>
<a href=http://c.biancheng.net/view/9355.html>JavaScript String对象</a>
</dd>
<dd>
<span class=channel-num>24</span>
<a href=http://c.biancheng.net/view/9356.html>JavaScript Array对象</a>
</dd>
<dd>
<span class=channel-num>25</span>
<a href=http://c.biancheng.net/view/9357.html>JavaScript Date对象</a>
</dd>
<dd>
<span class=channel-num>26</span>
<a href=http://c.biancheng.net/view/9358.html>JavaScript Math对象</a>
</dd>
<dd>
<span class=channel-num>27</span>
<a href=http://c.biancheng.net/view/9359.html>JavaScript RegExp对象</a>
</dd>
<dd>
<span class=channel-num>28</span>
<a href=http://c.biancheng.net/view/9360.html>JavaScript DOM</a>
</dd>
<dd>
<span class=channel-num>29</span>
<a href=http://c.biancheng.net/view/9361.html>JavaScript Element对象</a>
</dd>
<dd>
<span class=channel-num>30</span>
<a href=http://c.biancheng.net/view/9362.html>JavaScript attributes对象</a>
</dd>
<dd>
<span class=channel-num>31</span>
<a href=http://c.biancheng.net/view/9363.html>JavaScript BOM</a>
</dd>
<dd>
<span class=channel-num>32</span>
<a href=http://c.biancheng.net/view/9364.html>JavaScript Navigator对象</a>
</dd>
<dd>
<span class=channel-num>33</span>
<a href=http://c.biancheng.net/view/9365.html>JavaScript Screen对象</a>
</dd>
<dd>
<span class=channel-num>34</span>
<a href=http://c.biancheng.net/view/9366.html>JavaScript Location对象</a>
</dd>
<dd>
<span class=channel-num>35</span>
<a href=http://c.biancheng.net/view/9367.html>JavaScript History对象</a>
</dd>
<dd>
<span class=channel-num>36</span>
<a href=http://c.biancheng.net/view/9368.html>JavaScript定时器</a>
</dd>
<dd>
<span class=channel-num>37</span>
<a href=http://c.biancheng.net/view/9369.html>JavaScript异常处理</a>
</dd>
<dd>
<span class=channel-num>38</span>
<a href=http://c.biancheng.net/view/9370.html>JavaScript表单验证</a>
</dd>
<dd>
<span class=channel-num>39</span>
<a href=http://c.biancheng.net/view/9371.html>JavaScript动画</a>
</dd>
<dd>
<span class=channel-num>40</span>
<a href=http://c.biancheng.net/view/9372.html>JavaScript调试</a>
</dd>
<dd>
<span class=channel-num>41</span>
<a href=http://c.biancheng.net/view/9373.html>JavaScript闭包</a>
</dd>
<dd class=active>
<span class=channel-num>42</span>
<a href=http://c.biancheng.net/view/9374.html>JavaScript严格模式</a>
</dd>
<dd>
<span class=channel-num>43</span>
<a href=http://c.biancheng.net/view/9375.html>JavaScript解析JSON</a>
</dd>
<dd>
<span class=channel-num>44</span>
<a href=http://c.biancheng.net/view/9376.html>JavaScript Cookie</a>
</dd>
<dd>
<span class=channel-num>45</span>
<a href=http://c.biancheng.net/view/9377.html>JavaScript Ajax请求</a>
</dd>
<dd>
<span class=channel-num>46</span>
<a href=http://c.biancheng.net/view/9378.html>JavaScript类型转换</a>
</dd>
<dd>
<span class=channel-num>47</span>
<a href=http://c.biancheng.net/view/9379.html>JavaScript事件冒泡与事件捕获</a>
</dd>
<dd>
<span class=channel-num>48</span>
<a href=http://c.biancheng.net/view/9380.html>JavaScript事件委托</a>
</dd>
</div>
</div>
<div id=article-wrap>
<div id=article>
<div class=arc-info>
<span class=position><span class="iconfont iconfont-home2"></span> <a href=http://c.biancheng.net/>首页</a> &gt; <a href=http://c.biancheng.net/js/>JavaScript</a></span>
</div>
<div id=ggxc-position-bottom class="ggxc-box sf-hidden"></div>
<h1>JS严格模式（use strict）详解</h1>
<div class="pre-next-page clearfix"><span class="pre left"><a href=http://c.biancheng.net/view/9373.html><span class=icon>&lt;</span> 上一节</a></span><span class="next right"><a href=http://c.biancheng.net/view/9375.html>下一节 <span class=icon>&gt;</span></a></span></div>
<div id=ggxc-arctop-pc-1 class=ggxc-box><p class=small-image><a href="https://www.54benniao.com/c_course/?from=biancheng" target=_blank><img width=100% src=data:, class=sf-hidden></a><a href="https://www.54benniao.com/java_course/?from=biancheng" target=_blank><img width=100% src=data:, class=sf-hidden></a><p class=big-link><a href="http://c.biancheng.net/view/niz69i.html?from=arctop" target=_blank style=color:#D33428>一套完整的嵌入式开发学习路线（高薪就业版），知识全面，思路清晰，猛击这里免费领取！</a></p></div>
<div id=arc-body>由于 JavaScript 语法不够严谨，一直被人们所诟病，例如在使用一个变量时，可以不使用 var 关键字来提前声明（例如：<code>url = 'http://c.biancheng.net/';</code>），此时 JavaScript 解释器会自动为您创建这个变量。为了使代码更加严谨，JavaScript 中引入了严格模式，一旦使用了严格模式，将不再允许使用那些不严谨的语法。<br>
<h2>
什么是严格模式</h2>
严格模式是在 ECMAScript5（ES5）中引入的，在严格模式下，JavaScript 对语法的要求会更加严格，一些在正常模式下能够运行的代码，在严格模式下将不能运行。<br>
<br>
添加严格模式，主要有以下几个目的：<br>
<ul>
<li>
消除 JavaScript 语法中一些不合理、不严谨的地方；</li>
<li>
消除代码中一些不安全的地方，保证代码的安全运行；</li>
<li>
提高 JavaScript 程序的运行效率；</li>
<li>
为以后新版本的 JavaScript 做好铺垫。</li>
</ul>
<br>
目前，主流浏览器包括 IE10 及其之后的版本都已支持严格模式，JavaScript 正在向着更合理、更安全、更严谨的方向发展。<br>
<h2>
启用严格模式</h2>
要启用严格模式，您只需要在 JavaScript 脚本的开头添加<code> "use strict"; </code>或<code> 'use strict'; </code>指令即可，如下所示：<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="html sh_html snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_preproc>&lt;!DOCTYPE</span> <span class=sh_type>html</span><span class=sh_preproc>&gt;</span><li><span class=sh_keyword>&lt;html</span> <span class=sh_type>lang</span><span class=sh_symbol>=</span><span class=sh_string>"en"</span><span class=sh_keyword>&gt;</span><li><span class=sh_keyword>&lt;head&gt;</span><li>    <span class=sh_keyword>&lt;meta</span> <span class=sh_type>charset</span><span class=sh_symbol>=</span><span class=sh_string>"UTF-8"</span><span class=sh_keyword>&gt;</span><li>    <span class=sh_keyword>&lt;title&gt;</span>JavaScript<span class=sh_keyword>&lt;/title&gt;</span><li><span class=sh_keyword>&lt;/head&gt;</span><li><span class=sh_keyword>&lt;body&gt;</span><li>    <span class=sh_keyword>&lt;script&gt;</span><li>        "use strict";<li><span style=display:none>&nbsp;</span><li>        x = 'http://c.binacheng.net/'; // 此处报错：Uncaught ReferenceError: x is not defined at index.html:11<li>        console.log(x);<li>    <span class=sh_keyword>&lt;/script&gt;</span><li><span class=sh_keyword>&lt;/body&gt;</span><li><span class=sh_keyword>&lt;/html&gt;</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;JavaScript&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;script&gt;
        "use strict";

        x = 'http://c.binacheng.net/'; // 此处报错：Uncaught ReferenceError: x is not defined at index.html:11
        console.log(x);
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>
如果将<code> "use strict"; </code>指令添加到 JavaScript 程序的第一行，则表示整个脚本都会处于严格模式。如果在函数的第一行代码中添加<code> "use strict";</code>，则表示只在该函数中启用严格模式。如下例所示：<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="html sh_html snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_preproc>&lt;!DOCTYPE</span> <span class=sh_type>html</span><span class=sh_preproc>&gt;</span><li><span class=sh_keyword>&lt;html</span> <span class=sh_type>lang</span><span class=sh_symbol>=</span><span class=sh_string>"en"</span><span class=sh_keyword>&gt;</span><li><span class=sh_keyword>&lt;head&gt;</span><li>    <span class=sh_keyword>&lt;meta</span> <span class=sh_type>charset</span><span class=sh_symbol>=</span><span class=sh_string>"UTF-8"</span><span class=sh_keyword>&gt;</span><li>    <span class=sh_keyword>&lt;title&gt;</span>JavaScript<span class=sh_keyword>&lt;/title&gt;</span><li><span class=sh_keyword>&lt;/head&gt;</span><li><span class=sh_keyword>&lt;body&gt;</span><li>    <span class=sh_keyword>&lt;script&gt;</span><li>        x = 'http://c.binacheng.net/';<li>        console.log(x);<li><span style=display:none>&nbsp;</span><li>        function sayHello(){<li>            'use strict';<li>            str = 'welcome http://c.binacheng.net/'; // 调用 sayHello() 函数在此处报错：Uncaught ReferenceError: str is not defined at sayHello (index.html:14) at index.html:17<li>            console.log(str);<li>        }<li>        sayHello();<li>    <span class=sh_keyword>&lt;/script&gt;</span><li><span class=sh_keyword>&lt;/body&gt;</span><li><span class=sh_keyword>&lt;/html&gt;</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;JavaScript&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;script&gt;
        x = 'http://c.binacheng.net/';
        console.log(x);

        function sayHello(){
            'use strict';
            str = 'welcome http://c.binacheng.net/'; // 调用 sayHello() 函数在此处报错：Uncaught ReferenceError: str is not defined at sayHello (index.html:14) at index.html:17
            console.log(str);
        }
        sayHello();
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>
<p class=tip-box>
注意：<code>"use strict"; </code>或<code> 'use strict'; </code>指令只有在整个脚本第一行或者函数第一行时才能被识别，除了 IE9 以及更低的版本外，所有的浏览器都支持该指令。</p>
<h2>
严格模式中的变化</h2>
相对于普通模式来说，严格模式对 JavaScript 的语法都做了一些改变。<br>
<h4>
1、不允许使用未声明的变量</h4>
普通模式下，如果一个变量还没有声明，就直接拿来赋值，JavaScript 解释器会自动为您创建这个变量。而在严格模式下，则不允许这么做，所有变量在使用前必须显式的声明，否则将会抛出一个 ReferenceError 错误。<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="javascript sh_javascript snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_string>"use strict"</span><span class=sh_symbol>;</span><li><span style=display:none>&nbsp;</span><li>v <span class=sh_symbol>=</span> <span class=sh_number>1</span><span class=sh_symbol>;</span>        <span class=sh_comment>// 此处报错：Uncaught ReferenceError: v is not defined</span><li><span style=display:none>&nbsp;</span><li><span class=sh_keyword>for</span><span class=sh_symbol>(</span>i <span class=sh_symbol>=</span> <span class=sh_number>0</span><span class=sh_symbol>;</span> i <span class=sh_symbol>&lt;</span> <span class=sh_number>2</span><span class=sh_symbol>;</span> i<span class=sh_symbol>++)</span> <span class=sh_cbracket>{</span> <span class=sh_comment>// 此处报错：Uncaught ReferenceError: i is not defined</span><li><span class=sh_cbracket>}</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>"use strict";

v = 1;        // 此处报错：Uncaught ReferenceError: v is not defined

for(i = 0; i &lt; 2; i++) { // 此处报错：Uncaught ReferenceError: i is not defined
}</pre></div></div>
<h4>
2、不允许删除变量或函数</h4>
在严格模式下，如果您尝试删除一个变量或函数，则会抛出语法错误。而在普通模式下，虽然不会成功，但并不报错。<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="javascript sh_javascript snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_string>"use strict"</span><span class=sh_symbol>;</span><li><span style=display:none>&nbsp;</span><li><span class=sh_keyword>var</span> person <span class=sh_symbol>=</span> <span class=sh_cbracket>{</span>name<span class=sh_symbol>:</span> <span class=sh_string>"Peter"</span><span class=sh_symbol>,</span> age<span class=sh_symbol>:</span> <span class=sh_number>28</span><span class=sh_cbracket>}</span><span class=sh_symbol>;</span><li><span class=sh_keyword>delete</span> person<span class=sh_symbol>;</span>  <span class=sh_comment>// 此处报错：Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.</span><li><span style=display:none>&nbsp;</span><li><span class=sh_keyword>function</span> <span class=sh_function>sum</span><span class=sh_symbol>(</span>a<span class=sh_symbol>,</span> b<span class=sh_symbol>)</span> <span class=sh_cbracket>{</span><li>    <span class=sh_keyword>return</span> a <span class=sh_symbol>+</span> b<span class=sh_symbol>;</span><li><span class=sh_cbracket>}</span><li><span class=sh_keyword>delete</span> sum<span class=sh_symbol>;</span>  <span class=sh_comment>// 此处报错：Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>"use strict";

var person = {name: "Peter", age: 28};
delete person;  // 此处报错：Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.

function sum(a, b) {
    return a + b;
}
delete sum;  // 此处报错：Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.</pre></div></div>
<h4>
3、函数中不允许有同名的参数</h4>
在严格模式下，如果函数中有两个或多个同名参数，则会抛出语法错误，而在普通模式下则不会。<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="javascript sh_javascript snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_string>"use strict"</span><span class=sh_symbol>;</span><li><span style=display:none>&nbsp;</span><li><span class=sh_keyword>function</span> <span class=sh_function>square</span><span class=sh_symbol>(</span>a<span class=sh_symbol>,</span> a<span class=sh_symbol>)</span> <span class=sh_cbracket>{</span>     <span class=sh_comment>// 此处报错：Uncaught SyntaxError: Duplicate parameter name not allowed in this context</span><li>    <span class=sh_keyword>return</span> a <span class=sh_symbol>*</span> a<span class=sh_symbol>;</span><li><span class=sh_cbracket>}</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>"use strict";

function square(a, a) {     // 此处报错：Uncaught SyntaxError: Duplicate parameter name not allowed in this context
    return a * a;
}</pre></div></div>
<h4>
4、eval 语句的作用域是独立的</h4>
普通模式下，eval 语句的作用域取决于它所在的位置，而在严格模式下，eval 语句本身就是一个局部作用域，通过 eval 语句生成的变量只能在 eval 语句内使用。<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="javascript sh_javascript snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_string>"use strict"</span><span class=sh_symbol>;</span><li><span style=display:none>&nbsp;</span><li><span class=sh_predef_func>eval</span><span class=sh_symbol>(</span><span class=sh_string>"var x = 5; console.log(x);"</span><span class=sh_symbol>);</span><li>console<span class=sh_symbol>.</span><span class=sh_function>log</span><span class=sh_symbol>(</span>x<span class=sh_symbol>);</span>     <span class=sh_comment>// 此处报错：Uncaught ReferenceError: x is not defined</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>"use strict";

eval("var x = 5; console.log(x);");
console.log(x);     // 此处报错：Uncaught ReferenceError: x is not defined</pre></div></div>
<h4>
5、不允许使用 with 语句</h4>
在严格模式下，不允许使用 with 语句。<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="javascript sh_javascript snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_string>"use strict"</span><span class=sh_symbol>;</span><li><span style=display:none>&nbsp;</span><li><span class=sh_keyword>var</span> radius1 <span class=sh_symbol>=</span> <span class=sh_number>5</span><span class=sh_symbol>;</span><li><span class=sh_keyword>var</span> area1 <span class=sh_symbol>=</span> <span class=sh_predef_var>Math</span><span class=sh_symbol>.</span>PI <span class=sh_symbol>*</span> radius1 <span class=sh_symbol>*</span> radius1<span class=sh_symbol>;</span><li><span style=display:none>&nbsp;</span><li><span class=sh_keyword>var</span> radius2 <span class=sh_symbol>=</span> <span class=sh_number>5</span><span class=sh_symbol>;</span><li><span class=sh_keyword>with</span><span class=sh_symbol>(</span><span class=sh_predef_var>Math</span><span class=sh_symbol>)</span> <span class=sh_cbracket>{</span>        <span class=sh_comment>// 此处报错：Uncaught SyntaxError: Strict mode code may not include a with statement</span><li>    <span class=sh_keyword>var</span> area2 <span class=sh_symbol>=</span> PI <span class=sh_symbol>*</span> radius2 <span class=sh_symbol>*</span> radius2<span class=sh_symbol>;</span><li><span class=sh_cbracket>}</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>"use strict";

var radius1 = 5;
var area1 = Math.PI * radius1 * radius1;

var radius2 = 5;
with(Math) {        // 此处报错：Uncaught SyntaxError: Strict mode code may not include a with statement
    var area2 = PI * radius2 * radius2;
}</pre></div></div>
<h4>
6、不允许写入只读属性</h4>
在严格模式下，不允许为只读或不存在的属性赋值，否则会造成语法错误，而在普通模式下，虽然不会成功，但并不会报错。<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="javascript sh_javascript snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_string>"use strict"</span><span class=sh_symbol>;</span><li><span style=display:none>&nbsp;</span><li><span class=sh_keyword>var</span> person <span class=sh_symbol>=</span> <span class=sh_cbracket>{</span>name<span class=sh_symbol>:</span> <span class=sh_string>"Peter"</span><span class=sh_symbol>,</span> age<span class=sh_symbol>:</span> <span class=sh_number>28</span><span class=sh_cbracket>}</span><span class=sh_symbol>;</span><li><span style=display:none>&nbsp;</span><li><span class=sh_predef_func>Object</span><span class=sh_symbol>.</span><span class=sh_function>defineProperty</span><span class=sh_symbol>(</span>person<span class=sh_symbol>,</span> <span class=sh_string>"gender"</span><span class=sh_symbol>,</span> <span class=sh_cbracket>{</span>value<span class=sh_symbol>:</span> <span class=sh_string>"male"</span><span class=sh_symbol>,</span> writable<span class=sh_symbol>:</span> <span class=sh_keyword>false</span><span class=sh_cbracket>}</span><span class=sh_symbol>);</span><li>person<span class=sh_symbol>.</span>gender <span class=sh_symbol>=</span> <span class=sh_string>"female"</span><span class=sh_symbol>;</span> <span class=sh_comment>// 此处报错：Uncaught TypeError: Cannot assign to read only property 'gender' of object '#&lt;Object&gt;'</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>"use strict";

var person = {name: "Peter", age: 28};

Object.defineProperty(person, "gender", {value: "male", writable: false});
person.gender = "female"; // 此处报错：Uncaught TypeError: Cannot assign to read only property 'gender' of object '#&lt;Object&gt;'</pre></div></div>
<h4>
7、不允许使用八进制数</h4>
在严格模式下，不允许使用八进制数（以零为前缀的数字，例如 010、0377），而在普通模式下则可以。<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="javascript sh_javascript snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_string>"use strict"</span><span class=sh_symbol>;</span><li><span style=display:none>&nbsp;</span><li><span class=sh_keyword>var</span> x <span class=sh_symbol>=</span> <span class=sh_number>010</span><span class=sh_symbol>;</span> <span class=sh_comment>// 此处报错：Uncaught SyntaxError: Octal literals are not allowed in strict mode.</span><li>console<span class=sh_symbol>.</span><span class=sh_function>log</span><span class=sh_symbol>(</span><span class=sh_predef_func>parseInt</span><span class=sh_symbol>(</span>x<span class=sh_symbol>));</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>"use strict";

var x = 010; // 此处报错：Uncaught SyntaxError: Octal literals are not allowed in strict mode.
console.log(parseInt(x));</pre></div></div>
<h4>
8、不能在 if 语句中声明函数</h4>
在严格模式下，不能在 if 语句中声明函数，调用在 if 语句中定义的函数时，会提示函数未定义。<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="javascript sh_javascript snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_string>"use strict"</span><span class=sh_symbol>;</span><li><span class=sh_comment>//如果在if语句中声明函数，则会产生语法错误</span><li><span class=sh_keyword>if</span> <span class=sh_symbol>(</span><span class=sh_keyword>true</span><span class=sh_symbol>)</span> <span class=sh_cbracket>{</span><li>    <span class=sh_keyword>function</span> <span class=sh_function>demo</span><span class=sh_symbol>()</span> <span class=sh_cbracket>{</span> <span class=sh_comment>// 此处报错：Uncaught ReferenceError: demo is not defined</span><li>        console<span class=sh_symbol>.</span><span class=sh_function>log</span><span class=sh_symbol>(</span><span class=sh_string>"http://c.biancheng.net/"</span><span class=sh_symbol>);</span><li>    <span class=sh_cbracket>}</span><li><span class=sh_cbracket>}</span><li><span class=sh_function>demo</span><span class=sh_symbol>();</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>"use strict";
//如果在if语句中声明函数，则会产生语法错误
if (true) {
    function demo() { // 此处报错：Uncaught ReferenceError: demo is not defined
        console.log("http://c.biancheng.net/");
    }
}
demo();</pre></div></div>
<h4>
9、禁止使用 this 表示全局对象</h4>
在普通模式下，this 关键字表示全局对象 window，而在严格模式下，this关键字则表示 undefined。<br>
<div class=snippet-container style=undefined;><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style=display:none></div><pre class="javascript sh_javascript snippet-formatted sh_sourceCode"><ol class=snippet-num><li><span class=sh_string>"use strict"</span><span class=sh_symbol>;</span><li><span class=sh_keyword>var</span> name <span class=sh_symbol>=</span> <span class=sh_string>"http://c.biancheng.net/"</span><span class=sh_symbol>;</span><li><span class=sh_keyword>function</span> <span class=sh_function>demoTest</span><span class=sh_symbol>()</span> <span class=sh_cbracket>{</span><li>    console<span class=sh_symbol>.</span><span class=sh_function>log</span><span class=sh_symbol>(</span><span class=sh_keyword>this</span><span class=sh_symbol>);</span> <li><span class=sh_cbracket>}</span><li><span class=sh_function>demoTest</span><span class=sh_symbol>();</span></ol></pre><pre class="snippet-textonly sh_sourceCode" style=display:none>"use strict";
var name = "http://c.biancheng.net/";
function demoTest() {
    console.log(this); 
}
demoTest();</pre></div></div>
</div>
<div id=ggxc-weixin-arcbottom>
<p>关注公众号「<span class=col-green>站长严长生</span>」，在手机上阅读所有教程，随时随地都能学习。本公众号由<a class=col-link href=http://c.biancheng.net/view/8092.html target=_blank rel=nofollow>C语言中文网站长</a>亲自运营，长期更新，坚持原创。</p>
<p style=margin-top:12px;text-align:center>
<img src="" alt=公众号二维码 width=160><br>
<span class=col-green>微信扫码关注公众号</span>
</p>
</div>
<div class="pre-next-page clearfix"><span class="pre left"><a href=http://c.biancheng.net/view/9373.html><span class=icon>&lt;</span> 上一节</a></span><span class="next right"><a href=http://c.biancheng.net/view/9375.html>下一节 <span class=icon>&gt;</span></a></span></div>
<div id=nice-arcs class=box-bottom>
<h4>推荐阅读</h4>
<ul class=clearfix>
<li><a href=http://c.biancheng.net/view/niz69i.html title=一套完整的嵌入式开发学习路线（高薪就业版） target=_blank>一套完整的嵌入式开发学习路线（高薪就业版）</a></li>
<li><a href=http://c.biancheng.net/view/tnnfqo.html title=一套课程卖1万，TMD太贵了！ target=_blank>一套课程卖1万，TMD太贵了！</a></li>
<li><a href=http://c.biancheng.net/view/unnurw.html title=跑了3000公里，见了一位大佬 target=_blank>跑了3000公里，见了一位大佬</a></li>
<li><a href=http://c.biancheng.net/view/9434.html title=程序员职业发展路线图（完整版+珍藏版） target=_blank>程序员职业发展路线图（完整版+珍藏版）</a></li>
<li><a href=http://c.biancheng.net/view/9448.html title=技术NB的程序员就一定工资高吗？ target=_blank>技术NB的程序员就一定工资高吗？</a></li>
<li><a href=http://c.biancheng.net/view/9480.html title=SaaS模式到底是什么？ target=_blank>SaaS模式到底是什么？</a></li>
<li><a href=http://c.biancheng.net/view/9486.html title=并发和并行的区别（图解） target=_blank>并发和并行的区别（图解）</a></li>
<li><a href=http://c.biancheng.net/view/9622.html title=曝光一下阿里的工资待遇和职位级别 target=_blank>曝光一下阿里的工资待遇和职位级别</a></li>
<li><a href=http://c.biancheng.net/view/9644.html title=为什么中国的996干不过美国的955？ target=_blank>为什么中国的996干不过美国的955？</a></li>
<li><a href=http://c.biancheng.net/view/zin7a2.html title=冒死揭露一点真相 target=_blank>冒死揭露一点真相</a></li>
</ul>
</div>
</div>
</div>
</div>
<div id=footer class=clearfix>
<div class="info left">
<p>精美而实用的网站，分享优质编程教程，帮助有志青年。千锤百炼，只为大作；精益求精，处处斟酌；这种教程，看一眼就倾心。</p>
<p>
<a href=http://c.biancheng.net/view/8066.html target=_blank rel=nofollow>关于网站</a> <span>|</span>
<a href=http://c.biancheng.net/view/8092.html target=_blank rel=nofollow>关于站长</a> <span>|</span>
<a href=http://c.biancheng.net/view/8097.html target=_blank rel=nofollow>如何完成一部教程</a> <span>|</span>
<a href=http://c.biancheng.net/view/9648.html target=_blank rel=nofollow>公众号</a> <span>|</span>
<a href=http://c.biancheng.net/view/8093.html target=_blank rel=nofollow>联系我们</a> <span>|</span>
<a href=http://c.biancheng.net/sitemap/ target=_blank rel=nofollow>网站地图</a>
</p>
<p>Copyright ©2012-2022 biancheng.net, <a href=https://beian.miit.gov.cn/ target=_blank rel=nofollow style=color:#666>冀ICP备2022013920号</a>, <img height=13 src="" alt=公安备案图标><a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=13110202001352" target=_blank rel=nofollow style=color:#666>冀公网安备13110202001352号</a>
</p>
</div>
<img id=logo_bottom class=right src="" alt=底部Logo>
<span id=return-top><b>↑</b></span>
</div>
<div id=addweixin-widget>
<p>
<img src="" alt=微信交流群 width=120><br>
<span>加入微信交流群，一起学习不枯燥。内含一款搜索神器，免费下载全网书籍和视频。</span>
</p>
<span id=close-addweixin-widget class="iconfont iconfont-close"></span>
</div>
<span style=display:none></span>
